package com.neusoft.neuiotms.support.mongo.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.bean.LogBean;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.support.mongo.converter.LogConverter;
import com.neusoft.neuiotms.support.mongo.model.LogDocument;
import com.neusoft.neuiotms.support.mongo.repository.LogRepository;
import com.neusoft.neuiotms.support.service.LogService;

@Service
@ConditionalOnClass(MongoTemplate.class)
public class LogServiceImpl implements LogService {

    @Autowired
    private LogRepository logRepository;
    @Autowired
    private LogConverter logConverter;

    @Override
    public LogBean save(final LogBean bean) {
        final LogDocument doc = this.logConverter.toDoc(bean);
        this.logRepository.save(doc);
        return bean;
    }

    @Override
    public List<LogBean> search(final Searchable searchable) {
        final List<LogDocument> docs = this.logRepository.search(searchable);
        final List<LogBean> beans =
                docs.stream().map(doc -> this.logConverter.toBean(doc)).collect(Collectors.toList());
        return beans;
    }

    @Override
    public Page<LogBean> search(final Pageable pageable, final Searchable searchable) {
        final Page<LogDocument> docs = this.logRepository.search(pageable, searchable);
        final Page<LogBean> beans = docs.map(doc -> this.logConverter.toBean(doc));
        return beans;
    }

}
